<template>
  <el-container class="container">
    <el-header>
      <div> <img src="../assets/logo.png" alt=""> <span>商城后台管理系统 </span> </div>
      <el-button @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width=" isCollapse ? '64px':'200px' ">
        <div class="toggleBtn" @click="toggleBtn">||| </div>
        <el-menu unique-opened router :collapse-transition='false' :collapse='isCollapse' :default-active="activePath" background-color="#333744" text-color="#fff" active-text-color="#ffd04b">
          <el-submenu :index="item.id+ '' " v-for=" item  in  menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id+''] "></i>
              <span> {{item.authName}} </span>
            </template>
            <el-menu-item :index=" '/'+  item2.path" v-for=" item2 in item.children " :key="item2.id">
              <i class="el-icon-menu"></i>
              <span slot="title" @click="getactivePath('/'+  item2.path )"> {{item2.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut() {
      this.$router.push('/login')
      window.sessionStorage.clear()
    },
    toggleBtn() {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error('获取列表失败')
      }
      this.menuList = res.data
    },
    getactivePath(path) {
      window.sessionStorage.setItem('activePath', path)
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  height: 100%;
}
.el-header {
  line-height: 60px;
  background-color: #666666;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
  }
  span {
    font-size: 20px;
    color: #fff;
    margin-left: 10px;
  }
  img {
    width: 60px;
    height: 60px;
  }
  .el-button {
    font-size: 16px;
  }
}
.el-aside {
  background-color: #333744;
}
.toggleBtn {
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  color: #fff;
  background-color: #999999;
}
.el-menu {
  border-right: 0;
}
.el-main {
  height: 100%;
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
</style>
